import fitz  # PyMuPDF
import os
import wx
from lay.layer import MyFrame1


# xps_dir = "C:\\Users\\凡连文\\Desktop"
# output_dir = "C:\\Users\\凡连文\\Desktop"


# 引入弹框

class XpsToPdf(MyFrame1):

    def submitLeftDown(self,event):
        self.m_textCtrl1.AppendText('开始处理。。。。。' + os.linesep)
        output_dir = self.pdfVal
        xps_dir = self.xpsVal

        xps_files = [f for f in os.listdir(xps_dir) if f.endswith(".xps")]

        for xps_file in xps_files:
            xps_file_path = os.path.join(xps_dir, xps_file)

            pdf_file = os.path.splitext(xps_file)[0] + ".pdf"
            pdf_file_path = os.path.join(output_dir, pdf_file)

            doc = fitz.open(xps_file_path)
            pdf_bytes = doc.convert_to_pdf()

            with open(pdf_file_path, "wb") as pdf_out:
                pdf_out.write(pdf_bytes)

            self.m_textCtrl1.AppendText(xps_file+'->'+ pdf_file + os.linesep)
            print(f"{xps_file}  {pdf_file}")
        self.m_textCtrl1.AppendText('处理完毕' + os.linesep)
        self.m_textCtrl1.AppendText('--------------------------------------------------------------------' + os.linesep)


class App(wx.App):

    def OnInit(self):
        xps = XpsToPdf()
        xps.param_init()
        xps.Show()
        return True

    def OnExit(self):  # 退出
        print("退出")
        return 0


def form():
    app = App()
    app.MainLoop()
